package com.dongdongshop.controller;

import com.dk.data.R;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

@Controller
public class HttpClientController {


    @RequestMapping("weather")
    @ResponseBody
    public String Weather(){
        //可关闭的连接
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get=new HttpGet("http://apis.juhe.cn/simpleWeather/query?city=北京&key=966ff987b5f5b6d5e6437433e939ba87");
        CloseableHttpResponse response=null;
        String s = null;
        try {
            //响应
            response = client.execute(get);

            //状态码
            int statusCode = response.getStatusLine().getStatusCode();
            if(statusCode==200){
                //主体
                HttpEntity entity = response.getEntity();
               s= EntityUtils.toString(entity,"utf-8");
                System.out.println(s);

                //确保被关闭
                EntityUtils.consume(entity);
            }
//            System.out.println(statusCode);状态码
            return s;

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }




    @RequestMapping("weat")
    @ResponseBody
    public String getIpAddr(HttpServletRequest request) throws Exception{
        String cityname="北京市";
        StringBuilder sb=new StringBuilder();;
        try {
            String weather_url = "http://wthrcdn.etouch.cn/weather_mini?city="+cityname;

            URL url = new URL(weather_url);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            GZIPInputStream gzin = new GZIPInputStream(is);
            InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式，自定义编码
            BufferedReader reader = new BufferedReader(isr);
            String line = null;
            while((line=reader.readLine())!=null)
                sb.append(line+" ");
            reader.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

}

